package com.abel.web.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.abel.domain.AuthRequest;
import com.abel.manager.AuthenticationManager;
import com.abel.manager.exception.AuthenticationException;
import com.abel.web.validator.ForgotPasswordValidator;
import com.abel.web.view.ForgotPasswordViewBean;

public class AuthenticationHelpController extends BaseMultiActionController {

	private String forgotPasswordView;

	private String forgotPasswordViewSuccess;

	private AuthenticationManager authenticationManager;

	
	public ModelAndView forgotPassword(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HashMap<String, Object> commandModel = new HashMap<String, Object>();
		commandModel.put("forgotPasswordView", new ForgotPasswordViewBean());
		return new ModelAndView(getForgotPasswordView(),commandModel);
	}

	
	
	public ModelAndView processForgotPassword(HttpServletRequest request,
			HttpServletResponse response, ForgotPasswordViewBean viewBean)
			throws Exception {

		HashMap<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView mav = new ModelAndView();
		
		/*
		 * since the methods are custom methods, the validator has to be
		 * instantiated and validated manually, which works!
		 */
		BindException errors = this.bindObject(request, viewBean, new ForgotPasswordValidator());
		
		 //adding all errors
		if (errors.hasErrors()) {
			mav.setViewName(getForgotPasswordView());
			mav.addObject("forgotPasswordView", viewBean);
			mav.addAllObjects(errors.getModel());
			return mav;
			
		} else {
			// construct domain
			AuthRequest authRequest = new AuthRequest();
			authRequest.setUserName(viewBean.getUserName());
			authRequest.setEmail(viewBean.getEmail());
			
			boolean validateCredentials = false;
			try {
				
				validateCredentials = authenticationManager.validateForgotPasswordCredentials(authRequest);
			} 
			catch (AuthenticationException e) {

				e.printStackTrace();
				errors.rejectValue("", "error.null",null, "A system error occurred.  Please close the browser and try again.  In the problem persists, please contact the system administrator.");
				mav.addAllObjects(errors.getModel());
				modelMap.put("forgotPasswordView", new ForgotPasswordViewBean());
				mav.addAllObjects(modelMap);
				mav.setViewName(getForgotPasswordView());
				return mav;
				
			}
			if (validateCredentials) {

				modelMap.put("forgotPasswordView", new ForgotPasswordViewBean());
				mav.addAllObjects(modelMap);
				mav.setViewName(getForgotPasswordViewSuccess());
				return mav;

			} 
			else {

				errors.rejectValue("", "error.null",null, "We were not able to validate the credentials that you sent us.  Please contact the system administrator.");
				mav.addAllObjects(errors.getModel());
				modelMap.put("forgotPasswordView", viewBean);
				mav.addAllObjects(modelMap);
				mav.setViewName(getForgotPasswordView());
				return mav;

			}
		}
	}

	public String getForgotPasswordView() {
		return forgotPasswordView;
	}

	public void setForgotPasswordView(String forgotPasswordView) {
		this.forgotPasswordView = forgotPasswordView;
	}


	/**
	 * @param authenticationManager the authenticationManager to set
	 */
	public void setAuthenticationManager(AuthenticationManager authenticationManager) {
		this.authenticationManager = authenticationManager;
	}



	/**
	 * @return the forgotPasswordViewSuccess
	 */
	public String getForgotPasswordViewSuccess() {
		return forgotPasswordViewSuccess;
	}



	/**
	 * @param forgotPasswordViewSuccess the forgotPasswordViewSuccess to set
	 */
	public void setForgotPasswordViewSuccess(String forgotPasswordViewSuccess) {
		this.forgotPasswordViewSuccess = forgotPasswordViewSuccess;
	}





}
